package com.malou.client.ventas.serie;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;
import com.malou.client.util.ComboboxFieldBinding;
import com.malou.client.util.Utilities;
import com.malou.client.ventas.tienda.Tiendas;

public class SerieDialog extends Dialog {
	
	 private HiddenField<String> id;
	 private TextField<String> serie;
	 private TextField<String> descripcion;
	 private TextField<String> parametro;
	 private TextField<String> ultimonumero;
	 private NumberField longitudnumero;
	 private ComboBox<BeanModel> tienda;
	 private ComboBox<BeanModel> documento;
	 private Button guardar;
	 private Button cancelar;
	 public Status status;
	 private FormBinding formBindings;
	 private FormPanel panel;
	
	 private MalouConstants constants = GWT.create(MalouConstants.class);
	 
	 public SerieDialog(){
		super();
	    setModal(true);
		setSize(450, 260);
		setButtonAlign(HorizontalAlignment.LEFT);
		setButtons("");
		setResizable(false);	
		
		panel = new FormPanel();		
		panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    panel.setLabelWidth(110);
	    
	    formBindings = new FormBinding(panel, false);	    
	    addFields();
	         
	    setFocusWidget(serie);
	    
	    Component comp[]={serie,descripcion,parametro,ultimonumero,
	    		longitudnumero,tienda,documento,guardar};
		Utilities.Enter(comp);
	 }
	 
	 @Override
	  protected void createButtons() {
	    super.createButtons();
	    
	    if (constants == null) {
	    	constants = GWT.create(MalouConstants.class);
		}
	    
	    status = new Status();
	    status.setBusy(constants.guardando());
	    status.hide();
	    status.setAutoWidth(true);
	    getButtonBar().add(status);
	    
	    getButtonBar().add(new FillToolItem());
	    
	    cancelar = new Button(constants.cancelar(),IconHelper.createPath("images/icons/cancel.png"));
		cancelar.setSize(70, 25);
	    cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  SerieDialog.this.hide();
	      }

	    });

		guardar = new Button(constants.guardar(),IconHelper.createPath("images/icons/save_all.png"));
		guardar.setSize(70, 25);
	    
	    guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {	    	  
	    	 guardar();		 
	    	  
	      }
	    });
	    
	    addButton(guardar);
	    addButton(cancelar);	    
	    
	  }
	 protected boolean validate() {
		
		boolean ser = serie.isValid();
		boolean des = descripcion.isValid();
		boolean par = parametro.isValid();
		boolean ult = ultimonumero.isValid();
		boolean lon = longitudnumero.isValid();
		boolean tie = tienda.isValid();		
		boolean doc = documento.isValid();
	    return ser && des && par && ult && lon && tie && doc;
	  }
	 
	  public void reset(){
		  setIconStyle("icon-new");
		  setHeading(constants.serieNuevaSerie());
		  
		  id.clear();
		  serie.clear();
		  descripcion.clear();
		  parametro.clear();
		  ultimonumero.clear();
		  longitudnumero.clear();
		  
		  status.hide();
		  getButtonBar().enable();
	  }
	 
	  public void setStore(ListStore<BeanModel> store){
		  
		  formBindings.addFieldBinding(new FieldBinding(id, "id"));
		  formBindings.addFieldBinding(new FieldBinding(serie, "serie"));
		  formBindings.addFieldBinding(new FieldBinding(descripcion, "descripcion"));
		  formBindings.addFieldBinding(new FieldBinding(parametro, "parametro"));
		  formBindings.addFieldBinding(new FieldBinding(ultimonumero, "ultimonumero"));
		  formBindings.addFieldBinding(new FieldBinding(longitudnumero, "longitudnumero"));
		  formBindings.addFieldBinding(new ComboboxFieldBinding(tienda, "tienda"));
		  formBindings.addFieldBinding(new ComboboxFieldBinding(documento, "documento"));
		  
		  formBindings.setStore(store);
	  }
	 
	 private void addFields() {

			id = new HiddenField<String>();
			id.setVisible(false);
			panel.add(id);
			
			serie = new TextField<String>();
			serie.setMinLength(3);
			serie.setFieldLabel(constants.serieSerie());			    
		    panel.add(serie, new FormData("95%"));
		    
		    descripcion = new TextField<String>();
		    descripcion.setMinLength(4);
		    descripcion.setFieldLabel(constants.serieDescripcion());
		    panel.add(descripcion, new FormData("95%"));	       
		    
		    parametro = new TextField<String>();
		    parametro.setMinLength(3);		    
		    parametro.setFieldLabel(constants.serieParametro());		    
		    panel.add(parametro, new FormData("95%"));
		    
		    ultimonumero = new TextField<String>();
		    ultimonumero.setMinLength(3);	    
		    ultimonumero.setFieldLabel(constants.serieUltimonumero());	    
		    ultimonumero.setAllowBlank(false);
		    panel.add(ultimonumero, new FormData("50%"));
		    	    
		    longitudnumero = new NumberField();		        
		    longitudnumero.setFieldLabel(constants.serieLongitudnumero());
		    longitudnumero.setAllowBlank(false);
		    longitudnumero.setPropertyEditorType(Integer.class);
		    longitudnumero.setAllowNegative(false);
		    panel.add(longitudnumero, new FormData("50%"));
		    
		    Tiendas.getComboTienda();
		    Tiendas.loaderCombo.load(); // for edit
		    
		    tienda = new ComboBox<BeanModel>();  
		    tienda.setEditable(false);
		    tienda.setFieldLabel(constants.serieTienda());
		    tienda.setDisplayField("nombre");
		    tienda.setEmptyText("Seleccione...");
		    tienda.setWidth(150);  
		    tienda.setStore(Tiendas.storeCombo);  
		    tienda.setTypeAhead(true);  
		    tienda.setTriggerAction(TriggerAction.ALL);
		    tienda.setValueField("nombre");
		    tienda.setUseQueryCache(false);
		    panel.add(tienda, new FormData("95%"));
		    
		    Series.getComboDocumento();
		    Series.loaderCombo.load(); // for edit
		    
		    documento = new ComboBox<BeanModel>();  
		    documento.setEditable(false);
		    documento.setFieldLabel(constants.serieDocumento());
		    documento.setDisplayField("descripcion");
		    documento.setEmptyText("Seleccione...");
		    documento.setWidth(150);  
		    documento.setStore(Series.storeCombo);  
		    documento.setTypeAhead(true);  
		    documento.setTriggerAction(TriggerAction.ALL);
		    documento.setValueField("descripcion");	
		    documento.setUseQueryCache(false);
		   
		    panel.add(documento, new FormData("95%"));
		    
		    add(panel);
		    
	   }
	 
	  public void beforeEdit(){
		  setIconStyle("icon-update");
		  setHeading(constants.serieModificarSerie());		  
		  
		  status.hide();
		  getButtonBar().enable();
	  }
	  public void bind(BeanModel selectedModel){		  
		  formBindings.bind(selectedModel);
	  }
	  public void unbind(){
		  formBindings.unbind();
	  }
	  
	  private void guardar(){
		  
		  if(validate()){
	    		status.show();
		    getButtonBar().disable();
		    
		    Serie sr = new Serie();
			
			if(id.getValue() != null)
				sr.setId(Integer.valueOf(id.getValue()));
			sr.setSerie(serie.getValue());
			sr.setDescripcion(descripcion.getValue());
			sr.setParametro(parametro.getValue());
			sr.setUltimonumero(ultimonumero.getValue());
			if(Utilities.hasValue(longitudnumero))  			
				sr.setLongitudnumero(Integer.valueOf(longitudnumero.getValue().toString()));
			if(tienda.getValue()!= null)
			if(tienda.getValue().get("id") != null)
          	sr.setTienda_id(Integer.parseInt(tienda.getValue().get("id").toString()));
			
			if(documento.getValue()!= null)
			if(documento.getValue().get("id") != null)
          	sr.setTipodocumento_id(Integer.parseInt(documento.getValue().get("id").toString()));
			Series.serieDialog = SerieDialog.this;   			
			Series.save(sr);	    			
	    	}	    
		  
	  }
}
